import paddlex as pdx
from paddlex import transforms as T

# 定义数据处理和增强的变换
train_transforms = T.Compose([
    T.RandomResizeByShort(short_sizes=[640, 672, 704, 736, 768, 800], max_size=1333),
    T.RandomHorizontalFlip(),
    T.Normalize(
        mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

eval_transforms = T.Compose([
    T.ResizeByShort(short_size=800, max_size=1333),
    T.Normalize(
        mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# 定义数据集
train_dataset = pdx.datasets.ImageNet(
    data_dir='dataset',
    file_list='dataset/train_list.txt', #自动创建
    label_list='dataset/labels.txt', #自动创建
    transforms=train_transforms,
    shuffle=True)

eval_dataset = pdx.datasets.ImageNet(
    data_dir='dataset',
    file_list='dataset/val_list.txt',
    label_list='dataset/labels.txt',
    transforms=eval_transforms)

# 初始化模型
num_classes = len(train_dataset.labels)
model = pdx.cls.ResNet50_vd(num_classes=num_classes)

# 模型训练
model.train(
    num_epochs=10,
    train_dataset=train_dataset,
    train_batch_size=32,
    eval_dataset=eval_dataset,
    learning_rate=0.001,
    lr_decay_epochs=[4, 6, 8],
    save_dir='output/resnet50_vd',
    use_vdl=True)

# 模型评估
eval_metrics = model.evaluate(eval_dataset, batch_size=32, return_details=False)
print("评估结果:", eval_metrics)

# 模型预测
image_path = 'test_image.jpg'
result = model.predict(image_path)
print("预测结果:", result)

# 导出用于部署的模型
model.export_inference_model('inference_model')